package com.scu.servlet;

import com.scu.dao.GoodDao;
import com.scu.entity.Good;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


@WebServlet("/addServlet")

public class addServlet extends HttpServlet{
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //拿到前端表单中输入的数据
        String goodname = request.getParameter("goodname");
//        int id = request.getIntHeader("id");
        int year = request.getIntHeader("year");
        int month = request.getIntHeader("month");
        int day = request.getIntHeader("day");
        String username = request.getParameter("username");
        Good good = new Good();
        good.setGoodname(goodname);
//        good.setId(id);
        good.setYear(year);
        good.setMonth(month);
        good.setDay(day);
        good.setUsername(username);

        try {
            new GoodDao().insert(good);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //拿到前端表单中输入的数据
        String goodname = request.getParameter("goodname");
//        int id = request.getIntHeader("id");
        int year = request.getIntHeader("year");
        int month = request.getIntHeader("month");
        int day = request.getIntHeader("day");
        String username = request.getParameter("username");
        Good good = new Good();
        good.setGoodname(goodname);
//        good.setId(id);
        good.setYear(year);
        good.setMonth(month);
        good.setDay(day);
        good.setUsername(username);

        try {
            new GoodDao().insert(good);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
